﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel.Composition;
using System.IO.IsolatedStorage;
using ViewMaker.Core.Utils;
using System.Xml.Serialization;
using System.IO;
using System.Collections.Generic;


namespace ViewMaker.Core.Silverlight
{
    [Export(typeof(IViewLayoutPersistenceService))]
    public class SlViewLayoutPersistenceService : ViewLayoutPersistenceService
    {
        public override string GetDefaultLayoutName(Type vmType)
        {
            return TypeUtil.GetFullTypeString(vmType).Replace("<", "[").Replace(">", "]") + ".xml";
        }

        public override void Save(ViewLayoutItem layout, string filename)
        {
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (var st = store.CreateFile(filename))
                {
                    var ser = new XmlSerializer(typeof(ViewLayoutItem));
                    ser.Serialize(st, layout);
                }
            }
        }

        public override ViewLayoutItem Load(string filename)
        {
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!store.FileExists(filename)) return null;
                using (var st = store.OpenFile(filename, FileMode.Open, FileAccess.Read))
                {
                    var ser = new XmlSerializer(typeof(ViewLayoutItem));
                    return (ViewLayoutItem)ser.Deserialize(st);
                }
            }
        }

        public override void Delete(string filename)
        {
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!store.FileExists(filename)) return;
                store.DeleteFile(filename);
            }
        }

        public override bool Exists(string filename)
        {
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                return store.FileExists(filename);
            }
        }

        public override List<string> ListData()
        {
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                var ser = new XmlSerializer(typeof(ViewLayoutItem));
                var result = new List<string>();
                foreach (var file in store.GetFileNames("*.xml"))
                {
                    try
                    {
                        // Try Deserialize
                        using (var st = store.OpenFile(file, FileMode.Open, FileAccess.Read))
                        {
                            ser.Deserialize(st);
                        }
                        result.Add(file);
                    }
                    catch
                    {
                        //ERROR FREE
                    }
                }
                return result;
            }
        }

    }

}
